package priv.bestbeat.reportcenter.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import priv.bestbeat.reportcenter.core.monitor.TaskObservable;
import priv.bestbeat.reportcenter.core.monitor.mybatis.mapper.TaskObservableMybatisMapper;
import priv.bestbeat.reportcenter.core.mq.MessageProxy;
import priv.bestbeat.reportcenter.core.mq.rocket.RocketMessage;
import priv.bestbeat.reportcenter.core.task.OfflineTask;
import priv.bestbeat.reportcenter.core.task.Task;
import priv.bestbeat.reportcenter.core.utils.SnowflakeUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 张渠钦
 * @date 2022/4/28 9:31
 */
@RestController
@RequestMapping("/offline-task/")
public class OfflineTaskController {
    
    @Autowired
    private MessageProxy messageProxy;

    @Autowired
    private TaskObservableMybatisMapper taskObservableMybatisMapper;
    
    @PostMapping(value="/test")
    public ResponseEntity<Boolean> test() throws Exception {
        RocketMessage<OfflineTask> message = new RocketMessage<>();
        message.setTopic("taskA");
        message.setTags("tag1");
        OfflineTask offlineTask = new OfflineTask();
        offlineTask.setId(SnowflakeUtil.getId());
        Map<String,String> body = new HashMap<>();
        body.put("test1","09878");
        body.put("ddd","dsfe");
        offlineTask.setPayload(body);
        offlineTask.setName("测试任务");
        message.setPayload(offlineTask);
        messageProxy.sendWithMonitor(message);
        return ResponseEntity.ok(true);
    }

    @GetMapping("/{id}")
    public ResponseEntity<OfflineTask> get(@PathVariable("id") long id) {
        TaskObservable taskObservable = new TaskObservable();
        taskObservable.setId(id);
        Task task = (Task) taskObservableMybatisMapper.selectById(taskObservable).getObj();
        return ResponseEntity.ok((OfflineTask)task);
    }
}
